Changing SSID and Network Password

Latest update: July 2013

In this tutorial, we will show you how to view and change your FlashAir SSID and network password using config.cgi. We will use the set SSID operation to set a new SSID and the set network security key operation to set the new password.
Additionally, since we are displaying the current SSID and password, we will need to use command.cgi. We will use Get SSID and Get network password to fetch the current SSID and password respectively.

Creating the Screen Layout

This is what the screen layout of the application should look like:

this image shows the storyboard view of the screen layout for the entire app

When the Get button is tapped, the app displays the current SSID in the labelSSID block and the current password in the labelPassword block.
When the Set button is tapped, the app will segue to the View Controller of the registration screen where the new SSID and password can be entered. Once the user is finished filling out the text fields, the Done button can be tapped to submit the set operations to the FlashAir.

Warning: In order to set a new SSID or network password on your FlashAir device, you will need the MASTERCODE for your FlashAir device. This can be found by inserting your FlashAir into a PC or Mac and opening the CONFIG file of the FlashAir device. The CONFIG file is found in the "/SD_WLAN/" directory of the FlashAir device and should be opened in a text editor. You can check the Mastercode only when you finished FlashAir initial setting. There are hidden attributes attached to this directory, therefore we will use tools to handle this hidden folder.

We will add the following layout elements:

Display Screen

  • UIButton ( UIButton)
    • Get: Gets the current SSID and password
    • Set: Shows the screen where you can set a new SSID and password
  • Label ( UILabel)
    • labelSSID: Where the current SSID will display (after pressing the Get button)
    • labelPassword: Where the current password will display (after pressing the Get button)

Registration Screen

  • UIButton ( UIButton)
    • Done: Submits your new SSID and password to the FlashAir
  • Text View ( UITextField)
    • textMASTERCODE: Enter the current MASTERCODE here
    • textSSID: Enter a new SSID here
    • textPassword1: Enter a new password here
    • textPassword2: Type the new password again to avoid typos
  • View Controller ( UIViewController)
  • Scroll View ( UIScrollView)
    • Allows the user to see the input field while the keyboard is displayed on the screen

Other Elements

  • Navigation Controller ( UINavigationController)
    • Used to return to the display screen from the registration screen

The display screen looks like this:

this image shows the display screen

This is the screen that appears when the Set button is tapped. We will implement this class later in the tutorial.

  • FSSetViewController
this image shows the registration screen

Writing Code

Getting SSID and password

To get the current SSID and password, we use command.cgi with op=104 and op=105. We will use NSString stringWithContentsOfURL to execute the CGI command. This NSString function will allow us to get the information at the specified URL in UTF-8 encoding and will return any error data in an NSError object.

FSViewController.h

@interface FSViewController : UIViewController
- (IBAction)buttonGetPush:(id)sender;
@property (strong, nonatomic) IBOutlet UILabel *labelSSID;
@property (strong, nonatomic) IBOutlet UILabel *labelPassword;
@end

FSViewController.m

- (IBAction)buttonGetPush:(id)sender {

    NSError *error = nil;
    // Get SSID
    // Make url
    NSURL *url104 = [NSURL URLWithString:@"http://flashair/command.cgi?op=104"];
    // Run cgi
    NSString *SSIDStr = [NSString stringWithContentsOfURL:url104 
                                            encoding:NSUTF8StringEncoding error:&error];
    if ([error.domain isEqualToString:NSCocoaErrorDomain]) {
        UIAlertView *alert = [ [UIAlertView alloc] initWithTitle:self.title
                                 message:@"op=104 Failed \n check access point" delegate:nil
                                 cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        NSLog(@"error104 %@\n",error);
        return;
    }

    // Get Password
    // Make url
    NSURL *url105 = [NSURL URLWithString:@"http://flashair/command.cgi?op=105"];
    // Run cgi
    NSString *passwordStr = [NSString stringWithContentsOfURL:url105 
                                                encoding:NSUTF8StringEncoding error:&error];
    if ([error.domain isEqualToString:NSCocoaErrorDomain]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:self.title
                                message:@"op=105 Failed \n check access point" delegate:nil
                                          cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        NSLog(@"error105 %@\n",error);
        return;
    }
    self.labelSSID.text = SSIDStr;
    self.labelPassword.text = passwordStr;

}

The above function implements the action done by the app after the user presses the Get button - which is fetching the SSID and network password for the FlashAir.

  • Line 6:
    We set the URL used to retrieve the current SSID. We set op=104 for the CGI command.
  • Lines 8-9:
    We execute the CGI command and encode the returned characters. We specify UTF-8 encoding using encoding:NSUTF8StringEncoding.
  • Line 21:
    We set the URL used to fetch the current password. WE set op=104 for the CGI command.
  • Lines 23-24:
    We execute the CGI command and encode the returned characters. We specify UTF-8 encoding using encoding:NSUTF8StringEncoding.
  • Lines 33-34:
    We set labelSSID and labelPassword to display the string values of the fetched SSID and network password.

Setting the SSID and Password

To get the SSID and network password, we will use config.cgi with MASTERCODE, APPSSID, and APPNETWORKKEY as parameters. We will use NSString stringWithContentsOfURL again to execute the CGI command.

FSSetViewController.h

@interface FSSetViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *textMASTERCODE;
@property (strong, nonatomic) IBOutlet UITextField *textSSID;
@property (strong, nonatomic) IBOutlet UITextField *textPassword1;
@property (strong, nonatomic) IBOutlet UITextField *textPassword2;
- (IBAction)buttonDonePush:(id)sender;
@end

FSSetViewController.m

- (IBAction)buttonDonePush:(id)sender {

    NSError *error = nil;
    // Check
    NSCharacterSet *charSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];    
    // MASTERCODE
    NSString *mastercodeText = [self.textMASTERCODE.text 
                                                stringByTrimmingCharactersInSet:charSet];
    if ([mastercodeText isEqualToString:@""]){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:self.title
                                                message:@"Enter MASTERCODE" delegate:nil
                                      cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        return;
    }
    // SSID
    NSString *ssidText = [self.textSSID.text stringByTrimmingCharactersInSet:charSet];
    if ([ssidText isEqualToString:@""]){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:self.title
                                                message:@"Enter SSID" delegate:nil
                                      cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        return;
    }
    // password
    NSString *password1Text = [self.textPassword1.text 
                                                stringByTrimmingCharactersInSet:charSet];
    NSString *password2Text = [self.textPassword2.text 
                                                stringByTrimmingCharactersInSet:charSet];
    if ([password1Text isEqualToString:@""] || [password2Text isEqualToString:@""]){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:self.title
                                                message:@"Enter password" delegate:nil
                                      cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        return;
    }else if(![password1Text isEqualToString:password2Text]){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:self.title
                                                message:@"Password mismatch!" delegate:nil
                                      cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        return;
    }

    // Set SSID and password
    // Make url
    NSString *urlStr = [@"http://flashair/config.cgi?MASTERCODE=" 
                                                    stringByAppendingString:mastercodeText];
    urlStr = [urlStr stringByAppendingString:@"&APPNETWORKKEY="];
    urlStr = [urlStr stringByAppendingString:password1Text];
    urlStr = [urlStr stringByAppendingString:@"&APPSSID="];
    urlStr = [urlStr stringByAppendingString:ssidText];
    NSURL *url = [NSURL URLWithString:urlStr];
    // Run cgi
    NSString *rtnStr =[NSString stringWithContentsOfURL:url 
                                                encoding:NSUTF8StringEncoding error:&error];
    if ([error.domain isEqualToString:NSCocoaErrorDomain]){
        NSLog(@"config.cgi %@\n",error);
        return;
    }else{
        if([rtnStr isEqualToString:@"ERROR"]){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:self.title
                                  message:@"config.cgi failed" delegate:nil
                                  cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alert show];
            return;
        }
    }

    // Close this View
    [self.navigationController popToRootViewControllerAnimated:YES];

}

-(BOOL)textFieldShouldReturn:(UITextField*)textField{
    [textField resignFirstResponder];
    return YES;
}

We have now set the behavior of the new SSID and password entry screen and the Done button. Once the text field input has been checked, we will submit the new SSID and password.

  • Lines 5-42:
    Check the input in the text fields. If the input is not valid (does not pass the checks that we have set up), then an alert will show on the screen and the user will have to correct the input.
  • Lines 46-52:
    We set up the URL for the cgi command: config.cgi?MASTERCODE=<yourMastercode>&APPNETWORKKEY=<yourNewSSID>&APPSSID=<yourNewPassword>
  • Lines 54-55:
    The CGI is executed. The returned string is encoded in UTF-8 since we have specified encoding:NSUTF8StringEncoding.
  • Line 70:
    If the registration is successful, we will return to the first screen.
  • Line 74-77:
    This function sets the text field so that if "Done" is tapped on the input keyboard, the keyboard will close.

Result

Once we finish writing the program, we will check to see if it works.
First, we will try tapping the Get button.

The SSID and password of the FlashAir have been displayed:

this image shows the Get screen with the password and SSID displayed

Next, we will tap the Set button. In order to set a new SSID and password, we will need the MASTERCODE (discussed earlier in the tutorial). Check the CONFIG file of the FlashAir device for this value:

this image shows the MASTERCODE in the CONFIG file

Now I will enter the MASTERCODE, new SSID, and new password in their appropriate text fields.

Warning: Please be sure to remember the new SSID and password that you entered.
Remember, you will need to set a network security password. None of the fields can be left blank.
After you tap done, the SSID and password of the FlashAir have been changed. Therefore, your current Wireless LAN connection to the FlashAir is invalid and you will be disconnected from the FlashAir.

this image shows the Set screen with all text fields filled

Now I will press the Done button on the bottom of the screen.
Since you have changed the SSID and password, you will need to reconnect to the new SSID before you can use the FlashAir's Wireless LAN functionality again.
Since our command was successful, we have been returned to the Get screen. After connecting to the new SSID in our Wireless LAN settings, we will press the Get button.

The screen will now display our new SSID and network password:

this image shows the Get screen with the changed SSID and password

You have now completed the tutorial on getting and setting your SSID and network password.

Sample Code

ios_tutorial_06.zip (25KB)

All sample code on this page is licensed under BSD 2-Clause License